package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderAddressService;
import com.itheima.utils.AliSMSUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderaddress")
public class OrderAddressController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderAddressService orderAddressService;

    @PostMapping("/send4Order")
    public Result send4Order(String telephone){
        //随机生成数字
        String numeric = RandomStringUtils.randomNumeric(6);
        //发送验证码
        try {
            AliSMSUtil.sendMessageByAliSMS(telephone,numeric);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //保存到jedis中
        jedisPool.getResource().set("order:set"+telephone,numeric);
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Map map) throws Exception {
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        return orderAddressService.addOrder(map);
    }
    @GetMapping("/findAddress")
    public Result findAddress(){
        //查询所有医院
        List<Map<String,String>> list = null;
        try {
            list = orderAddressService.findAddress();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }

        return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,list);
    }
    //根据套餐id查询套餐信息
    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal =  orderAddressService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
